const { friendUrl, uniFriendUrl } = require('./enum')
const dayjs = require('dayjs')
const axios = require('axios')
async function getFriendList(lemmaId = 1180418, lemmaTitle = '迪丽热巴') {
    let result = {}
    try {
        const res = await axios({
            method: 'GET',
            url: friendUrl,
            params: {
                lemmaId,
                lemmaTitle,
            },
        })
        if (res.data.errno == 0) {
            result.friends = res.data.list
            result.success = true
        }
    } catch (e) {
        console.log(e)
        return { success: false } //代表接口数据获取失败
    }
    return result
}

async function addTimeGround(data) {
    const { lemmaId, lemmaTitle, coverUrl, posterUrlArr, _id } = data

    const banner = []
    if (coverUrl) {
        const coverRes = await uploadImg({
            lemmaId,
            imageUrl: height(coverUrl),
        })
        if (coverRes.code == 200) {
            banner.push(parseFileID(coverRes.data.fileID))
        }
    }

    const poster = []
    const len = Math.min(posterUrlArr.length, 10) // 改为单个日期最多上传10张照片
    for (let i = 0; i < len; i++) {
        const imageUrl = posterUrlArr[i]

        const upRes = await uploadImg({
            lemmaId,
            imageUrl: height(imageUrl),
        })
        if (upRes.code == 200) {
            poster.push(parseFileID(upRes.data.fileID))
        } else {
            console.log(`${lemmaTitle}第${i}张图片添加失败`)
        }
    }
    const params = {
        banner,
        poster,
        posterUpdated: true,
    }
    const res = await axios({
        method: 'POST',
        data: {
            _id,
            params,
        },
        url: uniFriendUrl.shareUpdate,
    })
    return poster
}

function parseTimePlace(baseInfo) {
    // 匹配年、月、日和出生地点的正则表达式
    const regex = /(\d{4})年(\d{1,2})月(\d{1,2})日/

    const placeRegex1 = /出生于([^，]+)，/
    const placeRegex2 = /生于([^，]+)，/
    const placeRegex3 = /出生在([^，]+)，/
    const placeRegex4 = /生在([^，]+)，/
    // 进行匹配
    let match = baseInfo.match(regex)

    // 输出匹配结果
    let year, month, day, birthTime
    if (match) {
        year = match[1]
        month = match[2]
        day = match[3]
        birthTime = dayjs(`${year}-${month}-${day}`).valueOf()
    }
    let placeMatch = baseInfo.match(placeRegex1) // 出生地点
    if (!placeMatch) placeMatch = baseInfo.match(placeRegex2)
    if (!placeMatch) placeMatch = baseInfo.match(placeRegex3)
    if (!placeMatch) placeMatch = baseInfo.match(placeRegex4)
    let birthPlace
    if (placeMatch) {
        birthPlace = placeMatch[1]
    }

    return { birthPlace, birthTime }
}
function isAddFn(birthPlace) {
    let isAdd = true
    let arr = ['美国', '韩国', '日本', '加拿大', '泰国', '印度尼西亚', '印度', '越南']
    for (let i = 0; i < arr.length; i++) {
        if (birthPlace.indexOf(arr[i]) > -1) {
            isAdd = false
            break
        }
    }
    return isAdd
}

async function updateFriend(data) {
    const res = await axios({
        method: 'POST',
        data,
        url: uniFriendUrl.update,
    }).catch((e) => e.response)
    return res.data
}

async function uploadImg(data) {
    let result = {}
    try {
        const res = await axios({
            method: 'POST',
            data,
            url: uniFriendUrl.upload,
        })
        result = res.data
    } catch (e) {}

    return result
}
function parseFileID(url) {
    let arr = url.split('/')
    let name = arr.pop()
    let extname = name.split('.').pop()
    return {
        url,
        name,
        extname,
    }
}
function height(imageUrl) {
    const replaceFunction = (match, p1, p2) => {
        if (p1 === 'h_') {
            return p1 + '4096'
        } else if (p1 === 'w_') {
            return p1 + '4096'
        } else if (p1 === 'limit_') {
            return p1 + '2'
        }
    }

    // 使用正则表达式进行替换
    const replacedUrl = imageUrl.replace(/(h_|w_|limit_)(\d+)/g, replaceFunction)

    return replacedUrl
}

module.exports = {
    getFriendList,
    parseTimePlace,
    addTimeGround,
    isAddFn,
    updateFriend,
    uploadImg,
    parseFileID,
}
